package com.zhihuishu.treenity.controller.course;


import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zhihuishu.micro.course.openapi.course.CourseSpeakerOpenService;
import com.zhihuishu.micro.course.openapi.course.dto.CourseClipsOpenDto;
import com.zhihuishu.micro.course.openapi.course.dto.CourseOpenDto;
import com.zhihuishu.micro.course.openapi.course.dto.CourseSpeakerOpenDto;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.treenity.controller.BaseController;
import com.zhihuishu.treenity.dto.user.UserDto;
import com.zhihuishu.treenity.log.LoggerCollectionEnum;
import com.zhihuishu.treenity.service.course.CourseService;

/***
 * @author User zhouzha
 *
 */
@Controller
@RequestMapping("/course/firstStep")
public class FirstStepController extends BaseController {
	
	@Resource
	private CourseService courseService;
	
	@Resource
	private CourseSpeakerOpenService courseSpeakerService;
	
    @Override
    protected LoggerCollectionEnum getCollectionEnum() {
        return LoggerCollectionEnum.firstStepCollection;
    }
	@ResponseBody
	@RequestMapping(" ")
	private ModelAndView index(HttpServletRequest request,@RequestParam(value="courseId",required=false)Long courseId) throws RemoteException {
		ModelAndView mv = new ModelAndView();
		mv.addObject("courseId", courseId);
		mv.setViewName("/course/firstStep");
		if(this.isLoginUserByCourseId(mv, courseId, request.getSession())){
			return mv;
		}
		
		return mv;
	}
	
	/**
	 * 根据课程Id 来修改相应的课程
	 * @param course
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "updateCourseInfo")
	public RemoteResult<Void> updateCourseInfo(HttpServletRequest request,final CourseOpenDto course){
		UserDto user = this.getLoginUser();
		final long updateUser = user.getUserId();
		 RemoteResult<Void> result = new ResultRequestAndParser<Void>("建课第一步", "treenity-firstStep", "修改课程基本信息", "updateCourseBasicInfo") {
	            @Override
	            public RemoteResult<Void> request(Long curUserId) throws RemoteException {
	            		RemoteResult<Void> rs = courseService.updateCourseInfo(course, updateUser);
	            		//如果修改的字段是课程名称，则删除缓存
	            		if(StringUtils.isNotBlank(course.getName())){
	            			courseService.delRedisCourseList(curUserId);
	            		}
		            	return rs;
	            }
	        }.get();
	        
	   return result;		
	}
	
	/**
	 * 根据课程Id 来查找所有的课程信息
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "queryCourseInfo")
	public RemoteResult<CourseOpenDto> queryCourseInfo(@RequestParam("courseId")long courseId){
		RemoteResult<CourseOpenDto>  courseOpenDto = new RemoteResult<CourseOpenDto>();
		try {
			courseOpenDto = courseService.loadCourse(courseId);
		} catch (RemoteException e) {
			log.error(e.getMessage());
		}
		return courseOpenDto;
	}
	
	/**
	 * 来查找所有的教师团队（根据 课程Id）
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "courseSpeakearList")
	public RemoteResult<List<CourseSpeakerOpenDto>> courseSpeakearList(@RequestParam("courseId") final long courseId){
        RemoteResult<List<CourseSpeakerOpenDto>> courseSpeakearList = new ResultRequestAndParser<List<CourseSpeakerOpenDto>>("建课业务", "treenity-firstStep", "查询教师团队列表", "courseSpeakearList") {
            @Override
            public RemoteResult<List<CourseSpeakerOpenDto>> request(Long curUserId) throws RemoteException {
        		RemoteResult<List<CourseSpeakerOpenDto>> result = courseSpeakerService.courseSpeakearList(courseId);
            	return result;
            }
        }.get();
		return courseSpeakearList;
	}
	
	
	/**
	 * 删除 教师团队（根据 id 来标志性）
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delCourseSpeaker")
	public RemoteResult<Void> delCourseSpeaker(@RequestParam("id") final Integer id){
        RemoteResult<Void> result = new ResultRequestAndParser<Void>("建课业务", "treenity-firstStep", "删除教师团队负责人", "delCourseSpeaker") {
            @Override
            public RemoteResult<Void> request(Long curUserId) throws RemoteException {
            	RemoteResult<Void> rs = courseSpeakerService.delete(id);;
            	return rs;
            }
        }.get();
		return result;
	}
	
	/**
	 * 修改教师团队
	 * @param courseSpeaker
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="updateSpeakerCourse")
	public RemoteResult<CourseSpeakerOpenDto> updateSpeakerCourse(final CourseSpeakerOpenDto courseSpeaker){
        RemoteResult<CourseSpeakerOpenDto> result = new ResultRequestAndParser<CourseSpeakerOpenDto>("建课业务", "treenity-firstStep", "修改教师团队", "updateSpeakerCourse") {
            @Override
            public RemoteResult<CourseSpeakerOpenDto> request(Long curUserId) throws RemoteException {
            	RemoteResult<CourseSpeakerOpenDto> rs = courseSpeakerService.update(courseSpeaker);
            	return rs;
            }
        }.get();
		return result;
	}
	
	/**
	 * 保存教师团队
	 * @param courseSpeaker
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "saveSpeakerCourse")
	public RemoteResult<CourseSpeakerOpenDto> saveSpeakerCourse(final CourseSpeakerOpenDto courseSpeaker) {
		if (courseSpeaker.getDecription().isEmpty() && courseSpeaker.getImg().isEmpty()
				&& courseSpeaker.getUsername().isEmpty() && courseSpeaker.getJobstatus().isEmpty()) {
			return null;
		}
		RemoteResult<CourseSpeakerOpenDto> result = new ResultRequestAndParser<CourseSpeakerOpenDto>("建课业务",
				"treenity-firstStep", "保存教师团队", "saveSpeakerCourse") {
			@Override
			public RemoteResult<CourseSpeakerOpenDto> request(Long curUserId) throws RemoteException {
				RemoteResult<CourseSpeakerOpenDto> rs = courseSpeakerService.save(courseSpeaker);
				return rs;
			}
		}.get();
		return result;
	}
	
	
	/**
	 * 上传片花
	 * @param request
	 * @param clipsOpen
	 * @param courseId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="saveClips")
	public RemoteResult<Long> saveClips(HttpServletRequest request,final CourseClipsOpenDto clipsOpen, final long courseId){
		UserDto user = this.getLoginUser();
		final long updateUser = user.getUserId();
		RemoteResult<Long> result = new ResultRequestAndParser<Long>("建课业务", "treenity-firstStep", "上传课程片花", "saveCourseVideo") {
            @Override
            public RemoteResult<Long> request(Long curUserId) throws RemoteException {
            	RemoteResult<Long> rs = courseService.saveClips(clipsOpen, courseId, updateUser);
            	return rs;
            }
        }.get();
		return result;
		
	}
	
	/**
	 * 修改片花
	 * @param clipsOpen
	 */
	@ResponseBody
	@RequestMapping(value="updateClips")
	public RemoteResult<Void> updateClips(final CourseClipsOpenDto clipsOpen){
		
		RemoteResult<Void> result = new ResultRequestAndParser<Void>("建课业务", "treenity-firstStep", "修改课程片花", "updateClips") {
            @Override
            public RemoteResult<Void> request(Long curUserId) throws RemoteException {
            	RemoteResult<Void> rs = courseService.UpdateClipseCourese(clipsOpen);
            	return rs;
            }
        }.get();
		return result;
		
	}
	
	/**
	 * 根据Id 来查找片花
	 * @param clipsId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="searchCourseClipse")
	public RemoteResult<CourseClipsOpenDto> searchCourseClipse(long clipsId){
		
		RemoteResult<CourseClipsOpenDto> clipsOpen = new RemoteResult<CourseClipsOpenDto>();
		try {
			clipsOpen = courseService.queryClipseOpenDto(clipsId);
		} catch (RemoteException e) {
		     log.error(e.getMessage());
		}
		return clipsOpen;
	}
	
	/**
	 * 根据VideoId更新片花封面
	 * @param clipsId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="updatePromoVideoImg")
	public RemoteResult<CourseClipsOpenDto> updatePromoVideoImg(HttpServletRequest request,@RequestParam(value="ids",required=false) final String ids,final CourseClipsOpenDto clipsOpen, final long courseId){
		UserDto user = this.getLoginUser();
		final long updateUser = user.getUserId();
		RemoteResult<CourseClipsOpenDto> result = new ResultRequestAndParser<CourseClipsOpenDto>("建课业务", "treenity-firstStep", "修改课程片花", "updateClips") {
            @Override
            public RemoteResult<CourseClipsOpenDto> request(Long curUserId) throws RemoteException {
            	RemoteResult<CourseClipsOpenDto> rs = courseService.updatePromoVideoImg(clipsOpen, courseId, updateUser);
            	return rs;
            }
        }.get();
		return result;
	}
	
}
